package com.dnake.misc;

import android.annotation.SuppressLint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

@SuppressLint("DefaultLocale")
public class SysCard {
	public static class Data {
		public int build;
		public int unit;
		public int floor;
		public int family;
		public int card;
	}

	public static int MAX = 10*10000;

	public static Data data[] = new Data[MAX];
	public static int max = 0;

	private static String url = "/dnake/cfg/sys_card.txt";

	private static void loadCard(String ss) {
		if (max >= MAX)
			return;

		StringTokenizer tk = new StringTokenizer(ss, " ");
		String s = tk.nextToken();
		String c = tk.nextToken();

		if (s == null || c == null || c.length() == 0)
			return;

		tk = new StringTokenizer(s, "-");
		String b = tk.nextToken();
		String u = tk.nextToken();
		String f = tk.nextToken();
		String r = tk.nextToken();
		if (b == null || u == null || r == null)
			return;

		int card = Integer.parseInt(c);
		if (card == 0)
			return;

		data[max] = new Data();
		data[max].build = Integer.parseInt(b);
		data[max].unit = Integer.parseInt(u);
		data[max].floor = Integer.parseInt(f);
		data[max].family = Integer.parseInt(r);
		data[max].card = card;
		max++;
	}

	public static Data verify(int card) {
		for(int i=0; i<max; i++) {
			if (data[i] != null && data[i].card == card)
				return data[i];
		}
		return null;
	}

	private static long save_ts = 0;

	public static void process() {
		if (save_ts != 0 && Math.abs(System.currentTimeMillis()-save_ts) >= 2000) {
			save_ts = 0;
			save();
		}
	}

	public static void rm(int card) {
		for(int i=0; i<max; i++) {
			if (data[i] != null && data[i].card == card) {
				max--;
				data[i] = data[max];

				save_ts = System.currentTimeMillis();
				break;
			}
		}
	}

	public static void add(Data d) {
		if (max >= MAX || d.card == 0)
			return;

		save_ts = System.currentTimeMillis();
		for(int i=0; i<max; i++) {
			if (data[i] != null && data[i].card == d.card) {
				data[i].build = d.build;
				data[i].unit = d.unit;
				data[i].floor = d.floor;
				data[i].family = d.family;
				return;
			}
		}
		data[max] = new Data();
		data[max].card = d.card;
		data[max].build = d.build;
		data[max].unit = d.unit;
		data[max].floor = d.floor;
		data[max].family = d.family;
		max++;
	}

	public static void load() {
		max = 0;
		for(int i=0; i<MAX; i++)
			data[i] = null;

		try {
			String s;
			FileInputStream in = new FileInputStream(url);
			BufferedReader r = new BufferedReader(new InputStreamReader(in));
			while ((s = r.readLine()) != null) {
				if (s.length() > 8)
					loadCard(s);
			}
			r.close();
			in.close();
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}

	public static void save() {
		try {
			FileOutputStream out = new FileOutputStream(url);
			for(int i=0; i<max; i++) {
				Data d = data[i];
				String s = String.format("%d-%d-%d-%d %d\n", d.build, d.unit, d.floor, d.family, d.card);
				out.write(s.getBytes());
			}
			out.flush();
			out.getFD().sync();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void reset() {
		max = 0;
		for(int i=0; i<MAX; i++)
			data[i] = null;
	}
}
